package edu.mobbuzz.rss.reader;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import edu.mobbuzz.daf.bean.Logs;
import edu.mobbuzz.daf.bean.Subscriber;
import edu.mobbuzz.daf.facade.LogsFacade;
import edu.mobbuzz.daf.facade.SubscriberFacade;
import edu.mobbuzz.messaging.Messaging;

public class FeedReader {  
	SyndFeed feed;  
	String url;  
	int numEntries;  
	FeedEntry entry[];  
	public String newline = System.getProperty("line.separator");  
	
	private LogsFacade logsFacade;
	private SubscriberFacade subscriberFacade;
	private Messaging messaging;
	
	public LogsFacade getLogsFacade() {
		if(logsFacade==null)
			logsFacade = new LogsFacade();
		return logsFacade;
	}
	
	
	public SubscriberFacade getSubscriberFacade() {
		if(subscriberFacade==null)
			subscriberFacade = new SubscriberFacade();
		return subscriberFacade;
	}


	public void setLogsFacade(LogsFacade logsFacade) {
		this.logsFacade = logsFacade;
	}

	public FeedReader(String _url, Messaging _messaging) {  
		url=_url;  
		messaging = _messaging;
		
		try {
			  
			feed=new SyndFeedInput().build(new XmlReader(new URL(url)));  
    		    	
			List entrl = feed.getEntries();  
			Object [] entries = entrl.toArray();  
			numEntries = entries.length;  
   
			entry = new FeedEntry[numEntries];  
			Logs logs = getLogsFacade().getLogs("url='"+url+"'");
			List<Subscriber> subscriberList = getSubscriberFacade().getSubscriberList("url='"+url+"'", "");
			
			for(int i=0; i< numEntries; i++) {  
				
				entry[i] = new FeedEntry((SyndEntryImpl)entries[i]);  
			    
				String s;  
				if(logs!=null){
					System.out.println("ADA");
					System.out.println(entry[i].getPubdate().toString());
					System.out.println(logs.getDate().toString());
					//if(entry[i].getPubdate().after(logs.getDate())){
						
						
						
						s="Title: "+entry[i].getTitle()+newline+  
					      "URL: "+entry[i].getLink()+newline+  
					      "Date: "+entry[i].getPubdate().toString()+newline+entry[i].getDescription()+newline;
						
						System.out.println(i+": "+s);
						if(subscriberList.size()!=0)
							sendNewsMessage(subscriberList,url, entry[i].getLink(), entry[i].getTitle(), entry[i].getDescription(), entry[i].getPubdate());
						
						
						if(i==0){							
							logs.setLink(entry[0].getLink());
							logs.setTitle(entry[0].getTitle());
							logs.setDescription(entry[0].getDescription());
							logs.setDate(entry[0].getPubdate());
							logs.setUrl(url);
							System.out.println("MASUKKAN "+entry[0].getPubdate().toString());
							getLogsFacade().updateLogs(logs);
						}
					//}
				}else{
					System.out.println("TIDAK ADA");	
					if(subscriberList.size()!=0)
						sendNewsMessage(subscriberList,url, entry[i].getLink(), entry[i].getTitle(), entry[i].getDescription(), entry[i].getPubdate());
					
					if(i==0){
						Logs _logs = new Logs();
						_logs.setLink(entry[0].getLink());
						_logs.setTitle(entry[0].getTitle());
						_logs.setDescription(entry[0].getDescription());
						_logs.setDate(entry[0].getPubdate());
						_logs.setUrl(url);
						getLogsFacade().addLogs(_logs);
					}
				}
				
				
			}  
			
		}  
		catch(Exception e) {  
			System.out.println("Exception in Feedreader: "+e.toString());  
			e.printStackTrace();  
		}  
   }
	
	public void sendNewsMessage(List subscriberList, String _url, String _link, String _title, String _description, Date _date){
		for (Iterator iterator = subscriberList.iterator(); iterator.hasNext();) {
			
			Subscriber subscriber = (Subscriber) iterator.next();
			
			System.out.println("MENGIRIM KE : "+ subscriber.getUsername());
			StringBuffer newsMessage = new StringBuffer("<news>");
			newsMessage.append("<url>"+_url+"</url>");
			newsMessage.append("<link>"+_link+"</link>");
			newsMessage.append("<title>"+_title+"</title>");
			newsMessage.append("<description>"+_description+"</description>");
			newsMessage.append("<date>"+_date.toString()+"</date>");
			newsMessage.append("</news>");
			
	        try {
				messaging.sendMessage(subscriber.getUsername(), newsMessage.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	   
}  
